package com.jdaoyun.gps.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jdaoyun.a_preAid.entity.GPSStatusModel;
import com.jdaoyun.a_preAid.service.AidAmbulanceService;
import com.jdaoyun.a_preAid.service.AidAmbulancemoduleService;
import com.jdaoyun.a_preAid.service.ParameterService;
import com.jdaoyun.config.ThirdPartyConfigure;
import com.jdaoyun.constant.ParameterConst;
import com.jdaoyun.gps.PositionService;
import com.jdaoyun.result.R;
import com.jdaoyun.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @Author: jkn
 * @CreateTime: 2023-08-18  10:52
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class PositionServiceImpl implements PositionService {

    @Autowired
    private ThirdPartyConfigure thirdPartyConfigure;

    @Autowired
    private RestTemplate restTemplate;


    @Override
    public List<GPSStatusModel> getGPSStatusModel(List<String> deviceFeature) {
        String url = thirdPartyConfigure.getAdress()+thirdPartyConfigure.getGetGpsStatus();
        Result<List<GPSStatusModel>> result = this.post(url, deviceFeature, new ParameterizedTypeReference<Result<List<GPSStatusModel>>>() {
        }).getBody();
        List<GPSStatusModel> data = result.getData();
        return data;
    }

    public <T> ResponseEntity<T> post(String url, Object requestBody, ParameterizedTypeReference<T> responseType) throws RestClientException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Accept-Charset", "UTF-8");
        HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
        return restTemplate.exchange(url, HttpMethod.POST, requestEntity, responseType);
    }

}
